package com.lab.webSocket.utils;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description: SESSION拦截器，用于验证用户身份
 * @Author: SN
 * @Date: 2017/2/22 14:52
 */
public class SessionInterceptor extends HandlerInterceptorAdapter {

    // 拦截前处理
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        String requestDevice = request.getParameter("requestDevice");//拦截设置
        //没有请求设置的情况下  判断loginSession 赋值为true反之为false  通过前端$.ajaxStep 发起行为
        if (null == requestDevice || "" == requestDevice) {
            HttpSession labTeacherLoginSession = request.getSession();
            if (labTeacherLoginSession.getAttribute("wsUserLoginSession") != null) {
                return true;
            } else {
                response.setHeader("sessionstatus", "timeout");
            }
        } else {
            return true;
        }
        return false;
    }

    // 拦截后处理
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView mav)
            throws Exception {
    }

    // 全部完成后处理
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception e)
            throws Exception {
    }
}
